<?php
/**
 ***********************************************************************************************************************
 * 网点相关服务实现
 ***********************************************************************************************************************
 */
namespace soa\express\shop;
use soa\client;

/**
 * Class match
 */
class match extends client
{
    /**
     * 服务地址
     * @var string
     */
    public $service_url = 'http://express.core.kuaidihelp.com/';

    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_express';

    /**
     * @var int
     */
    private $page = 1;
    /**
     * @var int
     */
    private $pageNum =30;
    /**
     * @var string
     */
    private $brand = '';
    /**
     * @var string
     */
    private $name = '';
    /**
     * @var array
     */
    private $geo = [];
    /**
     * @var string
     */
    private $address = '';
    /**
     * @var int
     */
    private $areaId = 0;
    /**
     * @var int
     */
    private $acceptOnlineOrder = 0;
    /**
     * @var int
     */
    private $verified = 0;

    /**
     * @param int $page
     * @return $this
     */
    public function page($page = 1)
    {
        $this->page = $page;
        return $this;
    }

    /**
     * @param int $pageNum
     * @return $this
     */
    public function pageNum($pageNum = 30)
    {
        $this->pageNum = !empty($pageNum) ? abs(intval($pageNum))  : 1;
        return $this;
    }

    /**
     * @param string $brand
     * @return $this
     */
    public function brand($brand = '')
    {
        $this->brand = $brand;
        return $this;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function name($name = '')
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @param float $lng
     * @param float $lat
     * @param int $range
     * @return $this
     */
    public function geo($lng = 0.0, $lat = 0.0, $range = 5)
    {
        $this->geo = [
            'lng' => $lng,
            'lat' => $lat,
            'range' => $range
        ];
        return $this;
    }

    /**
     * @param string $address
     * @return $this
     */
    public function address($address = '')
    {
        $this->address = $address;
        return $this;
    }

    /**
     * @param int $areaId
     * @return $this
     */
    public function areaId($areaId = 0)
    {
        $this->areaId = $areaId;
        return $this;
    }

    /**
     * @param int $acceptOnlineOrder
     * @return $this
     */
    public function acceptOnlineOrder($acceptOnlineOrder = 0)
    {
        $this->acceptOnlineOrder = $acceptOnlineOrder;
        return $this;
    }

    /**
     * @param int $verified
     * @return $this
     */
    public function verified($verified = 0)
    {
        $this->verified = $verified;
        return $this;
    }


    /**
     * @return bool|null
     */
    public function find()
    {
        // 设置默认错误码
        $this->_code = 1009;
        $this->_error = 1009;
        // 初始化条件参数
        $dict = [];

        // 页码
        if(empty($this->page) || !is_numeric($this->page) || !preg_match('/^[1-9]{1}[0-9]*$/', $this->page)) {
            $this->_msg = '[page]页码格式错误';
            return null;
        }
        $dict['page'] = $this->page;


        // 每页显示条数
        if(empty($this->pageNum) || !is_numeric($this->pageNum) || !preg_match('/^[1-9]{1}[0-9]*$/', $this->pageNum)) {
            $this->_msg = '[pageNum]每页显示条数格式错误';
            return null;
        }
        $dict['pageNum'] = $this->pageNum;


        // 每页显示条数
        if(!empty($this->brand) && !is_string($this->brand)) {
            $this->_msg = '[brand]品牌格式错误';
            return null;
        }
        $dict['brand'] = $this->brand;

        // 名称
        if(!empty($this->name) && !is_string($this->name)) {
            $this->_msg = '[name]名称格式错误';
            return null;
        }
        $dict['name'] = $this->name;

        // geo
        if(!empty($this->geo) &&
            (
                empty($this->geo['lng']) ||
                !is_float($this->geo['lng']) ||
                empty($this->geo['lat']) ||
                !is_float($this->geo['lat']) ||
                empty($this->geo['range']) ||
                !in_array($this->geo['range'], [3, 4, 5, 6, 7, 8])
            )
        ) {
            $this->_msg = '[geo]geo格式错误';
            return null;
        }
        $dict['geo'] = $this->geo;


        // 地址
        if(!empty($this->address) && !is_string($this->address)) {
            $this->_msg = '[address]地址格式错误';
            return null;
        }
        $dict['address'] = $this->address;

        // areaId
        if(!empty($this->areaId) && (!is_numeric($this->areaId) || !preg_match('/^[1-9]{1}[0-9]*$/', $this->areaId))) {
            $this->_msg = '[areaId]areaId格式错误';
            return null;
        }
        $dict['areaId'] = $this->areaId;


        // 是否接受订单设置
        if(!empty($this->acceptOnlineOrder) && intval($this->acceptOnlineOrder) != 1) {
            $this->_msg = '[acceptOnlineOrder]是否接受订单设置错误';
            return null;
        }
        $dict['acceptOnlineOrder'] = $this->acceptOnlineOrder;


        // 是否通过认证设置
        if(!empty($this->verified) && intval($this->verified) != 1) {
            $this->_msg = '[acceptOnlineOrder]是否通过认证设置错误';
            return null;
        }
        $dict['verified'] = $this->verified;


        // condition
        if(empty($dict)) {
            $this->_msg = '[condition]匹配条件不能为空';
            return null;
        }

        // 重置错误码
        $this->_code = 1001;
        $this->_error = 0;
        // 重置条件
        $this->resetCondition();

        // 进行匹配
        return $this->post('v1/shop/match/index', ['condition' => json_encode($dict)]);
    }


    /**
     * 重置请求条件
     */
    private function resetCondition()
    {
        $this->page = 1;
        $this->pageNum = 30;
        $this->brand = '';
        $this->name = '';
        $this->acceptOnlineOrder = 0;
        $this->address = '';
        $this->areaId = 0;
        $this->geo = [];
        $this->verified = 0;
    }

}